package cn.sixmillions.mb.sqlSession;

import cn.sixmillions.mb.pojo.Configuration;

import java.util.List;

/**
 *
 */
public class DefaultSqlSession implements SqlSession {

    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <E> List<E> selectList(String statementId, Object... params) throws Exception {
        //执行sql，拿到返回结果
        Executor simpleExecutor = new SimpleExecutor();
        List<E> query = simpleExecutor.query(configuration, configuration.getMappedStatementMap().get(statementId), params);
        return query;
    }

    @Override
    public <T> T selectOne(String statementId, Object... params) throws Exception {
        List<T> list = selectList(statementId, params);
        if (list.size() == 1) {
            return list.get(0);
        } else {
            throw new RuntimeException("查询结果为空或者多条");
        }
    }
}
